package mx.edu.itschapala.ssrp.bean;


import java.util.List;
import java.util.Map;
import javax.ejb.EJB;
import javax.enterprise.context.ApplicationScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.inject.Named;
import mx.edu.itschapala.ssrp.ln.CatalogoLN;
import mx.edu.itschapala.ssrp.ln.ConnectLN;
import mx.edu.itschapala.ssrp.modelo.Personal;
import mx.edu.itschapala.ssrp.modelo.Usuario;

@Named(value = "personalBean")
@ApplicationScoped
public class PersonalBean {

  @EJB
    private ConnectLN connectLN;
    @EJB
    private CatalogoLN catalogoLN;
    private String accion;
    private List<Personal> personales;
    private Personal personal;
    private String user;
    /**
     * Creates a new instance of DatosPersonal
     */
    public PersonalBean() {
    }

    public List<Personal> getPersonales() {
        
        personales = catalogoLN.getPersonales();
        return personales;
    }
    public Personal getPersonal() {
        return personal;
    }

    public void setPersonal(Personal personal) {
        this.personal = personal;
    }

   public String prepararNuevo() {
        accion = "nuevo";
        personal = new Personal();
        return "PersonalCrearEditar";
    }
   public void prepararEditar(ActionEvent evt) {
        accion = "editar";
        String idtemp = evt.getComponent().getClientId();
        System.out.println(">>>>>>>>>>>"+idtemp);
        int id = Integer.parseInt(idtemp.split(":")[2]);
        System.out.println(">>>>>>>>>>>>>>>>>>>>>>>"+personales.size());
        personal = personales.get(id);
        System.out.println(">>>>>>>>>>>>>>>>>>>>"+personal.getNombre());
    }
   
    public void prepararRemover(ActionEvent evt) {
        accion = "remover";
        String idtemp = evt.getComponent().getClientId();
        int id = Integer.parseInt(idtemp.split(":")[2]);
       personal = personales.get(id);
    }

    public String procesarAccion() {
        ExternalContext externalContext = 
FacesContext.getCurrentInstance().getExternalContext();
        Map<String, Object> sessionMap = 
externalContext.getSessionMap();
        Usuario logueado = (Usuario) sessionMap.get
("USUARIO_LOGUEADO");
        user = logueado.getPerfil();
        if (accion.equals("nuevo")) {
            personal.setNombre(personal.getNombre().toUpperCase());
            personal.setPuesto(personal.getPuesto().toUpperCase());
           personal.setCarrera(personal.getCarrera().toUpperCase());
            
            catalogoLN.crearPersonal(personal);
        } else if (accion.equals("editar")) {
            personal.setNombre(personal.getNombre().toUpperCase());
            personal.setPuesto(personal.getPuesto().toUpperCase());
            personal.setCarrera(personal.getCarrera().toUpperCase());
            
              catalogoLN.editarPersonal(personal);
        } else if (accion.equals("remover")) {
            personal.setNombre(personal.getNombre().toUpperCase());
            personal.setPuesto(personal.getPuesto().toUpperCase());
            personal.setCarrera(personal.getCarrera().toUpperCase());
            
              catalogoLN.removerPersonal(personal);
        }

        
            return "PersonalLista";
        
    }

    public String getUser() {
        return user;
    }
 public String getAccion() {
        return accion;
 }   
}